home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Win32 / OLE.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  9.8 KB  |  431 lines

  1.  
  2. package Win32::OLE;
  3.  
  4. $VERSION = '0.03';
  5. use Exporter;
  6. use DynaLoader;
  7. @ISA = qw( Exporter DynaLoader );
  8.  
  9. @EXPORT = qw(
  10.         Variant
  11.         VT_UI1
  12.         VT_I2
  13.         VT_I4
  14.         VT_R4
  15.         VT_R8
  16.         VT_DATE
  17.         VT_BSTR
  18.         VT_CY
  19.         VT_BOOL
  20.         );
  21. @EXPORT_OK = qw(
  22.         VT_EMPTY
  23.         VT_NULL
  24.         VT_DISPATCH
  25.         VT_ERROR
  26.         VT_VARIANT
  27.         VT_UNKNOWN
  28.         VT_UI2
  29.         VT_UI4
  30.         VT_I8
  31.         VT_UI8
  32.         VT_INT
  33.         VT_UINT
  34.         VT_VOID
  35.         VT_HRESULT
  36.         VT_PTR
  37.         VT_SAFEARRAY
  38.         VT_CARRAY
  39.         VT_USERDEFINED
  40.         VT_LPSTR
  41.         VT_LPWSTR
  42.         VT_FILETIME
  43.         VT_BLOB
  44.         VT_STREAM
  45.         VT_STORAGE
  46.         VT_STREAMED_OBJECT
  47.         VT_STORED_OBJECT
  48.         VT_BLOB_OBJECT
  49.         VT_CF
  50.         VT_CLSID
  51.         TKIND_ENUM
  52.         TKIND_RECORD
  53.         TKIND_MODULE
  54.         TKIND_INTERFACE
  55.         TKIND_DISPATCH
  56.         TKIND_COCLASS
  57.         TKIND_ALIAS
  58.         TKIND_UNION
  59.         TKIND_MAX
  60.            );
  61.  
  62. bootstrap Win32::OLE;
  63.  
  64.  
  65. *Win32::OLELastError = \&Win32::OLE::LastError;
  66. *Win32::OLECreateObject = \&Win32::OLE::CreateObject;
  67. *Win32::OLEDestroyObject = \&Win32::OLE::DestroyObject;
  68. *Win32::OLEDispatch = \&Win32::OLE::Dispatch;
  69. *Win32::OLEGetProperty = \&Win32::OLE::GetProperty;
  70. *Win32::OLESetProperty = \&Win32::OLE::SetProperty;
  71.  
  72.  
  73. sub AUTOLOAD {
  74.     my( $self ) = shift;
  75.     my $fReturn = "";
  76.     $AUTOLOAD =~ s/.*:://;
  77.     if ( Win32::OLE::Dispatch( $self, $AUTOLOAD, $fReturn, @_ ) ) {
  78.         return $fReturn;
  79.     } else {
  80.         return undef;
  81.     }
  82. }
  83.  
  84.  
  85.  
  86. sub VT_EMPTY {0;}
  87. sub VT_NULL {1;}
  88. sub VT_I2 {2;}
  89. sub VT_I4 {3;}
  90. sub VT_R4 {4;}
  91. sub VT_R8 {5;}
  92. sub VT_CY {6;}
  93. sub VT_DATE {7;}
  94. sub VT_BSTR {8;}
  95. sub VT_DISPATCH {9;}
  96. sub VT_ERROR {10;}
  97. sub VT_BOOL {11;}
  98. sub VT_VARIANT {12;}
  99. sub VT_UNKNOWN {13;}
  100. sub VT_I1 {16;}
  101. sub VT_UI1 {17;}
  102. sub VT_UI2 {18;}
  103. sub VT_UI4 {19;}
  104. sub VT_I8 {20;}
  105. sub VT_UI8 {21;}
  106. sub VT_INT {22;}
  107. sub VT_UINT {23;}
  108. sub VT_VOID {24;}
  109. sub VT_HRESULT {25;}
  110. sub VT_PTR {26;}
  111. sub VT_SAFEARRAY {27;}
  112. sub VT_CARRAY {28;}
  113. sub VT_USERDEFINED {29;}
  114. sub VT_LPSTR {30;}
  115. sub VT_LPWSTR {31;}
  116. sub VT_FILETIME {64;}
  117. sub VT_BLOB {65;}
  118. sub VT_STREAM {66;}
  119. sub VT_STORAGE {67;}
  120. sub VT_STREAMED_OBJECT {68;}
  121. sub VT_STORED_OBJECT {69;}
  122. sub VT_BLOB_OBJECT {70;}
  123. sub VT_CF {71;}
  124. sub VT_CLSID {72;}
  125.  
  126.  
  127.  
  128. sub TKIND_ENUM {0;}
  129. sub TKIND_RECORD {1;}
  130. sub TKIND_MODULE {2;}
  131. sub TKIND_INTERFACE {3;}
  132. sub TKIND_DISPATCH {4;}
  133. sub TKIND_COCLASS {5;}
  134. sub TKIND_ALIAS {6;}
  135. sub TKIND_UNION {7;}
  136. sub TKIND_MAX {8;}
  137.  
  138. sub new {
  139.     my( $object );
  140.     my( $c ) = shift;
  141.     my( $type ) = shift;
  142.     if ( CreateObject( $type, $object ) ) {
  143.         return $object;
  144.     } else {
  145.         return undef;
  146.     }
  147. }
  148.  
  149. sub Variant {
  150.     return Win32::OLE::Variant->new(@_);
  151. }
  152.  
  153.  
  154. *OLECreateObject = \&new;
  155.  
  156. package Win32::OLE::Variant;
  157.  
  158. sub new {
  159.     my $self = {};
  160.     my $pack = shift;
  161.     $self->{'Type'} = shift;
  162.     $self->{'Value'} = shift;
  163.     return bless $self, $pack;
  164. }
  165.  
  166. 1;
  167.  
  168. __END__
  169.  
  170. =head1 NAME
  171.  
  172. Win32::OLE - OLE Automation extensions and Variants
  173.  
  174. =head1 SYNOPSIS
  175.  
  176.     $ex = new Win32::OLE 'Excel.Application' or die "oops\n";
  177.     $ex->Amethod("arg")->Bmethod->{'Property'} = "foo";
  178.  
  179. =head1 DESCRIPTION
  180.  
  181. This module provides an interface to OLE Automation from Perl.
  182. OLE Automation brings VisualBasic like scripting capabilities and
  183. offers powerful extensibility and the ability to control many Win32
  184. applications from Perl scripts.
  185.  
  186. OCX's are currently not supported.
  187.  
  188. =head2 Functions/Methods
  189.  
  190. =over 8
  191.  
  192. =item new Win32::OLE $oleclass
  193.  
  194. OLE Automation objects are created using the new() method, the
  195. second argument to which must be the OLE class of the application
  196. to create.  Return value is undef if the attempt to create an
  197. OLE connection failed for some reason.
  198.  
  199. The object returned by the new() method can be used to invoke
  200. methods or retrieve properties in the same fashion as described
  201. in the documentation for the particular OLE class (eg. Microsoft
  202. Excel documentation describes the object hierarchy along with the
  203. properties and methods exposed for OLE access).
  204.  
  205. Properties can be retrieved or set using hash syntax, while methods
  206. can be invoked with the usual perl method call syntax.
  207.  
  208. If a method or property returns an embedded OLE object, method
  209. and property access can be chained as shown in the examples below.
  210.  
  211. =item Variant(TYPENAME, DATA)
  212.  
  213. This function returns a Win32::OLE::Variant object of the specified
  214. type that contains the given data.  The Win32::OLE::Variant object
  215. can be used to specify data types other than IV, NV or PV (which are
  216. supported transparently).  See L<Variants> below for details.
  217.  
  218. =back
  219.  
  220. =head2 Constants
  221.  
  222. These constants are exported by default:
  223.  
  224.     VT_UI1
  225.     VT_I2
  226.     VT_I4
  227.     VT_R4
  228.     VT_R8
  229.     VT_DATE
  230.     VT_BSTR
  231.     VT_CY
  232.     VT_BOOL
  233.  
  234. Other OLE constants are also defined in the Win32::OLE package,
  235. but they are unsupported at this time, so they are exported
  236. only on request:
  237.  
  238.     VT_EMPTY
  239.     VT_NULL
  240.     VT_DISPATCH
  241.     VT_ERROR
  242.     VT_VARIANT
  243.     VT_UNKNOWN
  244.     VT_UI2
  245.     VT_UI4
  246.     VT_I8
  247.     VT_UI8
  248.     VT_INT
  249.     VT_UINT
  250.     VT_VOID
  251.     VT_HRESULT
  252.     VT_PTR
  253.     VT_SAFEARRAY
  254.     VT_CARRAY
  255.     VT_USERDEFINED
  256.     VT_LPSTR
  257.     VT_LPWSTR
  258.     VT_FILETIME
  259.     VT_BLOB
  260.     VT_STREAM
  261.     VT_STORAGE
  262.     VT_STREAMED_OBJECT
  263.     VT_STORED_OBJECT
  264.     VT_BLOB_OBJECT
  265.     VT_CF
  266.     VT_CLSID
  267.  
  268. =head2 Variants
  269.  
  270. A Variant is a data type that is used to pass data between OLE
  271. connections.
  272.  
  273. The default behavior is to convert each perl scalar variable into
  274. an OLE Variant according to the internal perl representation.
  275. The following type correspondence holds:
  276.  
  277.         C type          Perl type       OLE type
  278.         ------          ---------       --------
  279.           int              IV            VT_I4
  280.         double             NV            VT_R8
  281.         char *             PV            VT_BSTR
  282.  
  283. Note that VT_BSTR is a wide character or Unicode string.  This presents a
  284. problem if you want to pass in binary data as a parameter as 0x00 is
  285. inserted between all the bytes in your data. The C<Variant()> method
  286. provides a solution to this.  With Variants the script
  287. writer can specify the OLE variant type that the parameter should be
  288. converted to.  Currently supported types are:
  289.  
  290.         VT_UI1     unsigned char
  291.         VT_I2      signed int (2 bytes)
  292.         VT_I4      signed int (4 bytes)
  293.         VT_R4      float      (4 bytes)
  294.         VT_R8      float      (8 bytes)
  295.         VT_DATE    OLE Date
  296.         VT_BSTR    OLE String
  297.         VT_CY      OLE Currency
  298.         VT_BOOL    OLE Boolean
  299.  
  300. When VT_DATE and VT_CY objects are created, the input
  301. parameter is treated as a Perl string type, which is then converted
  302. to VT_BSTR, and finally to VT_DATE of VT_CY using the VariantChangeType()
  303. OLE API function.  See L<EXAMPLES> for how these types can be used.
  304.  
  305. =head1 EXAMPLES
  306.  
  307. Here is a simple Microsoft Excel application.
  308.  
  309.     use Win32::OLE;
  310.     $ex = new Win32::OLE 'Excel.Application' or die "oops\n";
  311.     
  312.     $ex->Workbooks->Open( 'test.xls' );
  313.     
  314.     $ex->Workbooks(1)->Worksheets('Sheet1')->Cells(1,1)->{Value} = "foo";
  315.     
  316.     $ex->Save;
  317.     $ex->Quit;
  318.  
  319. Here is an example of using Variant data types.
  320.  
  321.     use Win32::OLE;
  322.     $ex = new Win32::OLE 'Excel.Application' or die "oops\n";
  323.     $ex->{Visible} = 1;
  324.     $ex->Workbooks->Add;
  325.     $ovR8 = Variant(VT_R8, "3 is a good number");
  326.     $ex->Range("A1")->{Value} = $ovR8;
  327.     $ex->Range("A2")->{Value} = Variant(VT_DATE, 'Jan 1,1970');
  328.  
  329. The above will put value "3" in cell A1 rather than the string
  330. "3 is a good number".  Cell A2 will contain the date.
  331.  
  332. Similarly, to invoke a method with some binary data, you can
  333. do the following:
  334.  
  335.     $obj->Method( Variant(VT_UI1, "foo\000b\001a\002r") );
  336.  
  337. Here is a wrapper class that basically delegates everything but
  338. new() and DESTROY().  Such a wrapper is needed for properly
  339. shutting down connections if your application is liable to
  340. die without proper cleanup.
  341.  
  342.     package Excel;
  343.     use Win32::OLE;
  344.     
  345.     sub new {
  346.         my $s = {};
  347.         if ($s->{Ex} = Win32::OLE->new('Excel.Application')) {
  348.         return bless $s, shift;
  349.         }
  350.         return undef;
  351.     }
  352.     
  353.     sub DESTROY {
  354.         my $s = shift;
  355.         if (exists $s->{Ex}) {
  356.         print "# closing connection\n";
  357.         $s->{Ex}->Quit;
  358.         return undef;
  359.         }
  360.     }
  361.     
  362.     sub AUTOLOAD {
  363.         my $s = shift;
  364.         $AUTOLOAD =~ s/^.*:://;
  365.         $s->{Ex}->$AUTOLOAD(@_);
  366.     }
  367.     
  368.     1;
  369.  
  370. The above module can be used just like Win32::OLE, except that
  371. it takes care of closing connections in case of abnormal exits.
  372.  
  373. =head1 NOTES
  374.  
  375. There are some incompatibilities with the version distributed by Activeware
  376. (as of build 306).
  377.  
  378. =over 4
  379.  
  380. =item 1
  381.  
  382. The package name has changed from "OLE" to "Win32::OLE".
  383.  
  384. =item 2
  385.  
  386. All functions of the form "Win32::OLEFoo" are now "Win32::OLE::Foo",
  387. though the old names are temporarily accomodated.
  388.  
  389. =item 3
  390.  
  391. Package "OLE::Variant" is now "Win32::OLE::Variant".
  392.  
  393. =item 4
  394.  
  395. The Variant function is new, and is exported by default.  So are
  396. all the VT_XXX type constants.
  397.  
  398. =back
  399.  
  400. You are responsible for properly closing any open OLE servers
  401. down.  For example, if you open a OLE connection to Excel and
  402. subsequently just die(), Excel will not shutdown and you will have
  403. a process leak on your hands.  You will need to wrap the OLE
  404. connection in your own object and provide a DESTROY method that
  405. does proper cleanup to ensure smooth shutdown.  Alternatively,
  406. you can use a __DIE__ hook or an END{} block to do such cleanup.
  407. See L<EXAMPLES> above for an example of using a wrapper object.
  408.  
  409. =head1 AUTHORS
  410.  
  411. Originally put together by the kind people at Hip and Activeware.
  412.  
  413. Gurusamy Sarathy <gsar@umich.edu> has subsequently fixed several
  414. major bugs, memory leaks, and reliability problems, along with some
  415. redesign of the code.
  416.  
  417. =head1 COPYRIGHT
  418.  
  419.     (c) 1995 Microsoft Corporation. All rights reserved. 
  420.     Developed by ActiveWare Internet Corp., http://www.ActiveWare.com
  421.  
  422.     Other modifications (c) 1997 by Gurusamy Sarathy <gsar@umich.edu>
  423.  
  424.     You may distribute under the terms of either the GNU General Public
  425.     License or the Artistic License, as specified in the README file.
  426.  
  427.  
  428. =cut
  429.  
  430.  
  431.